<template>
  <div class="search-box">
    <i class="icon-search"></i>
    <input type="text"
           class="box"
           :placeholder="placeholder"
           v-model="query"
           ref="input">
    <i v-show="query"
       class="icon-dismiss"
       @click="clear"></i>
  </div>
</template>

<script>
import { debounce } from 'common/js/utils'

export default {
  name: 'search-box',
  props: {
    placeholder: {
      type: String,
      default: '搜索歌曲或歌手'
    }
  },
  data () {
    return {
      query: ''
    }
  },
  methods: {
    clear () {
      this.query = ''
    },
    setQuery (query) {
      this.query = query
    },
    blurInput () {
      this.$refs.input.blur()
    }
  },
  created () {
    this.$watch('query', debounce((newQuery) => {
      this.$emit('query', newQuery)
    }, 250))
  }
}
</script>

<style lang="less" scoped>
@import "~common/css/variable";
.search-box {
  display: flex;
  align-items: center;
  box-sizing: border-box;
  width: 100%;
  padding: 0 6px;
  height: 40px;
  background-color: @color-highlight-background;
  border-radius: 6px;
  .icon-search {
    font-size: 24px;
    color: @color-background;
  }
  .box {
    flex: 1;
    margin: 0 5px;
    line-height: 18px;
    background-color: @color-highlight-background;
    color: @color-text;
    font-size: @font-size-medium;
    outline: none;
    border: none;
    border-radius: 6px;
    &::placeholder {
      color: @color-text-d;
    }
  }
  .icon-dismiss {
    font-size: 16px;
    color: @color-background;
  }
}
</style>
